var api = require("../../../utils/mr_xu/api_url.js").api
var request = require("../../../utils/mr_xu/request.js")
var common = require("../../../utils/mr_xu/common.js")
var config = require("../../../utils/mr_xu/config.js")
var orderCache = require("../../../utils/mr_xu/apply_order_cache.js")
const app = getApp()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    cancel_fee: 1000,
    free_cancel_limit_time: '',
    item: null,
    payed: false,
    load: false,
    businessConfig: {
      1: "将车送到指定地点",
      2: "仅找车",
      3: "找车且送到自定地点"
    },
    // 外层的1,2,3 对应business
    statusConfig: common.statusConfig,
    toIndex: 0,
    id: '',
    config_distance_limit: 0, //距离超过这个数，才能补差
    config_time_limit: '', //补差的限时
    need_more: false, //是否需要补差
    timer: '', //定时器名字
    showTime: '',
    showLimit:true,
    watchCostScores:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    let toIndex = options.toIndex ? 1 : 0
    let fromway = options.fromway
    let scores = common.getConfig('QUEST_DETAIL_DEDUCT_SCORE')
    this.setData({
      watchCostScores: scores
    })

    let id = options.id
    id = id ? id : 'e3e9f3f8bb544a4398435cbf8450d513'

    let that = this
    that.setData({
      id: id,
    })

    if (fromway == 'personal') {
      that.setData({
        payed: true,
      })
    }
    this.setConfig()
    this.initGetData(id, toIndex)

  },

  setConfig() {
    let settings = wx.getStorageSync('settings')
    for (let i in settings) {
      if (settings[i].key == 'QUEST_DIFERENCE_DISTANCE') {
        this.setData({
          config_distance_limit: settings[i].value
        })
      }
      if (settings[i].key == 'QUEST_DIFFERENCE_TIME') {
        this.setData({
          config_time_limit: settings[i].value
        })
      }
    }
  },

  onShow() {
    this.initGetData(this.data.id, this.data.toIndex)
  },
  setShowTime() {
    let showTime = this.data.showTime
    let item = this.data.item

    if (item.carriedTime)
      showTime = item.carriedTime
    else if (item.differenceTime)
      showTime = item.differenceTime
    else if (item.buckleTime)
      showTime = item.buckleTime
    else if (item.findTime)
      showTime = item.findTime
    else
      showTime = item.createTime

    this.setData({
      showTime:showTime
    })
  },
  initGetData(id, toIndex, toDetail) {
    let that = this
    let url = ''
    let payed = that.data.payed

    if (payed || toDetail) {
      url = api.apply_order_detail_payed
    } else {
      url = api.apply_order_detail
    }

    request.post(url, {
      data: id
    }, function(res) {

      let item = res.obj
      let showLimit = res.obj.limitTime && common.strtotime(res.obj.limitTime) > (new Date()).getTime() / 1000 ? true : false

      that.setData({
        item: item,
        load: true,
        toIndex: toIndex ? toIndex : 0,
        payed: url == api.apply_order_detail_payed && payed == false ? true : payed,
        showLimit: showLimit,
        need_more: true
      })

      that.setShowTime()

    })
  },
  toDetail() {
    let that = this
    wx.showModal({
      title: '注意',
      content: '查看将会消耗' + that.data.watchCostScores + '积分，是否继续',
      cancelColor: '#007AFF',
      confirmColor: '#007AFF',
      success: function(res) {
        if (res.confirm) {
          that.initGetData(that.data.id, that.data.toIndex, true)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  toReceive() {
    let that = this
    wx.navigateTo({
      url: '/pages/agency/quest/more_clue?id=' + that.data.id,
    })
  },
  toIndex() {
    wx.reLaunch({
      url: '/pages/agency/index',
    })
  },
  preview(e) {
    let image = e.target.dataset.image
    if (image) {
      wx.previewImage({
        urls: [image],
      })
    }
  },
  buckleCar() {

    let that = this
    request.post(api.buckle_car, {
      data: that.data.id
    }, function(res) {
      wx.showToast({
        title: res.msg,
      })
      that.initGetData(that.data.id)
    })

  },
  haveSendCar() {
    let that = this
    app.http.post(api.have_send_car, {
      data: this.data.id
    }).then(res => {
      wx.showToast({
        title: res.msg,
      })
      that.initGetData(that.data.id)
    })

  },
  moreMoney() {
    let that = this
    if (!that.data.item.destinationCoords) {
      wx.showToast({
        title: '无目的地信息',
        icon: 'null'
      })
      return
    }
    wx.navigateTo({
      url: './more_money?id=' + that.data.id + '&destinationCoords=' + that.data.item.destinationCoords,
    })
  },
  //粘贴/扣着  代表确认开始
  confirm_start() {
    let that = this
    app.http.post(api.buckle_car, {
        data: that.data.item.id
      })
      .then(res => {
        wx.showToast({
          title: res.msg,
          icon: 'null'
        })
        that.initGetData(that.data.id)
      })
  },
  look_in_map(e) {
    let coords = e.target.dataset.coords
    if (!coords) {
      coords = e.currentTarget.dataset.coords
    }
    let latitude = coords.split(',')[1]
    let longitude = coords.split(',')[0]

    wx.openLocation({
      latitude: parseFloat(latitude),
      longitude: parseFloat(longitude),
    })
  }

})